/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月11日         Root.Lu
 */

package com.coocaa.app.gamecenter.pages.detail;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.coocaa.app.gamecenter.pages.detail.DetailActivity.DetailData.BigPoster;
import com.coocaa.app.gamecenter.pages.detail.DownloadCtrView.DownloadCtrViewListener;
import com.coocaa.x.app.framework.app.LiteAppApplication;
import com.coocaa.x.framework.app.CoocaaActivity;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.Android;
import com.coocaa.x.framework.utils.LiteHandlerThread;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.framework.utils.UIUtils;
import com.coocaa.x.service.XServiceManager;
import com.coocaa.x.service.litecache.data.client.LiteImageCache.LiteImageCacheObject;
import com.coocaa.x.uipackage.BaseView;
import com.coocaa.x.uipackage.dialog.ImageGallery;
import com.coocaa.x.uipackage.utils.FrescoUtils;
import com.coocaa.x.uipackage.widget.CoocaaDraweeView;
import com.coocaa.x.uipackage.widget.SlideFocusView;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.data.SkyAppDetailData.AppStatus;
import com.tianci.appstore.ui.util.UICommon;
import com.tianci.appstore.ui.widget.TipToast;
import com.tianci.appstore.utils.SkyConstants;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class DetailView extends BaseView implements SlideFocusView.ISlideFocusView {
    public static enum SUPPORT_DEVICE {
        JOYSTICK(1 << 0), MOUSE(1 << 1), REMOTE(1 << 2), MOBLIE(1 << 3), INDUCTION_LEAP_MOTION(
                1 << 4), CONTROL_LEAP_MOTION(1 << 5), GUN(1 << 6);
        public int flag = 0;

        private SUPPORT_DEVICE(int flag) {
            this.flag = flag;
        }

        public boolean isSupport(int device) {
            return ((device & flag) > 0);
        }
    }

    public static interface OnBigPosterClickListener {
        void onBigPosterClick(BigPoster url);
    }

    private static final int _info_textsize = SkyConstants.Dpi(28);
    private static final int _intro_textsize = SkyConstants.Dpi(26);

    private static final int _bg_width = SkyConstants.Div(1920);
    private static final int _bg_height = SkyConstants.Div(1080);
    private static final int _linear_appinfo_root_width = SkyConstants.Div(1180);
    private static final int _linear_appinfo_content_width = SkyConstants.Div(932);
    private static final int _linear_bigposter_root_width = SkyConstants.Div(740);

    private static final int _frame_gc_detail_maininfo_x = SkyConstants.Div(0);
    private static final int _frame_gc_detail_maininfo_y = SkyConstants.Div(162);

    private static final int _info_line_space = SkyConstants.Div(20);
    private static final int _info_device_space = SkyConstants.Div(34);

    private static final int _poster_width = SkyConstants.Div(256);// SkyConstants.Div(454);
    private static final int _poster_height = SkyConstants.Div(256);

    private static final int _posterplay_width = SkyConstants.Div(90);
    private static final int _posterplay_height = SkyConstants.Div(90);

    private static final int _device_width = SkyConstants.Div(50);
    private static final int _device_height = SkyConstants.Div(50);
    private static final int _device_spacing = SkyConstants.Div(20);
    private static final int _bigposter_width = SkyConstants.Div(688);
    private static final int _bigposter_height = SkyConstants.Div(386);
    private static final int _bigposter_spacing = SkyConstants.Div(20);
    private static final int _bigposter_scrollerview_margin_top = SkyConstants.Div(38);
    private static final int _bigposter_scrollerview_padding_left = SkyConstants.Div(26);
    private static final int _bigposter_scrollerview_padding_right = SkyConstants.Div(26);
    private static final int _bigposter_scrollerview_margin_bottom = SkyConstants.Div(38);

    private static final int _frame_gc_detail_dlctr_margin_top = SkyConstants.Div(44);
    private static final int _frame_gc_detail_appinfoad_margin_top = SkyConstants.Div(94);
    private static final int _linear_gc_detail_intro_margin_top = SkyConstants.Div(58);
    private static final int _linear_gc_detail_appinfoleft_margin_left = SkyConstants.Div(82);
    private static final int _linear_gc_detail_appinfo_device_margin_top = SkyConstants.Div(34);

//    private LiteImageCache imageCache = LiteImageCache.getInstance();

    private Context mContext = null;
    private boolean isDestroyed = false;

    private FrameLayout frameRoot, frameBigPoster, frameAppinfoIntro, frameAppinfoAD, frameDLCtr,
            frameBigPosterRoot;
    private LinearLayout linearMain, linearAppinfoRoot, linearAppinfoMain, linearAppinfoMainLeft,
            linearAppinfoMainRight, linearDevices, linearAppinfoDevice, linearAppInfoContent;

    private CoocaaDraweeView imgPoster, imgPosterPlay, imgJoystick, imgMouse, imgRemote, imgMobile, imgGun,
            imgCLeapMotion, imgILeapMotion, imgBG;
    private TextView txtIntro;
    private RelativeLayout relativeBigPoster;
    private ScrollView scrollBiposter;

    private DownloadCtrView dlCtrView = null;
    private OnBigPosterClickListener bigPosterClickListener = null;

    private List<LiteImageCacheObject> imageCacheObjects = new ArrayList<LiteImageCacheObject>();
    //    private GCShotDialog shotDialog = null;
    private List<BigPoster> bigPosterUrls = null;

    public DetailView(Context context) {
        super(context);
        mContext = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.gamecenter_detail, this);

        initView();
    }

    private void initView() {
        frameRoot = (FrameLayout) findViewById(R.id.frame_gc_detail_root);
        imgBG = (CoocaaDraweeView) findViewById(R.id.img_gc_detail_bg);
        imgBG.setAlpha(5);
        UIUtils.setViewSize(imgBG, _bg_width, _bg_height);
//        imgBG.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        initAppinfoView();
        initBigposterView();
    }

    private void initBigposterView() {
        frameBigPosterRoot = (FrameLayout) findViewById(R.id.frame_gc_detail_bigposter_root);
        frameBigPosterRoot.setBackgroundColor(Color.argb(77, 0, 0, 0));

        UIUtils.setViewSize(frameBigPosterRoot, _linear_bigposter_root_width, -1);
        // UIUtils.setLayoutParams(frameBigPosterRoot, frameRoot, -1, -1,
        // _linear_bigposter_root_width, -1, -1, -1, -1, -1);
        relativeBigPoster = (RelativeLayout) findViewById(R.id.relative_gc_detail_bigposter);
        relativeBigPoster.setPadding(0, 0, SkyConstants.Div(20), 0);
        scrollBiposter = (ScrollView) findViewById(R.id.scroll_gc_detail_bigposter);
        setThumbV(R.drawable.game_center_progress_bar, scrollBiposter);
        UIUtils.setViewMargin(scrollBiposter, _bigposter_scrollerview_padding_left,
                _bigposter_scrollerview_margin_top, _bigposter_scrollerview_padding_right, _bigposter_scrollerview_margin_bottom);
//        scrollBiposter.setPadding(0, 0, 50, 0);
        // scrollBiposter.setPadding(_bigposter_scrollerview_padding_left,
        // _bigposter_scrollerview_margin_top, _bigposter_scrollerview_padding_right,
        // _bigposter_scrollerview_margin_bottom);// _bigposter_scrollerview_padding_top,
        // 0);//
        // _bigposter_scrollerview_padding_bottom);
    }

    private void initAppinfoView() {
        linearMain = (LinearLayout) findViewById(R.id.linear_gc_detail_appinfo_main);
        linearAppinfoRoot = (LinearLayout) findViewById(R.id.linear_gc_detail_appinfo_root);
        UIUtils.setViewSize(linearAppinfoRoot, _linear_appinfo_root_width, -1);
        UIUtils.setViewPosition(linearAppinfoRoot, _frame_gc_detail_maininfo_x,
                _frame_gc_detail_maininfo_y);
        // UIUtils.setLayoutParams(linearAppinfoRoot, linearMain, _frame_gc_detail_maininfo_x,
        // _frame_gc_detail_maininfo_y, _linear_appinfo_root_width, -1, -1, -1, -1, -1);
        initAppinfoMain();
        initAppinfoIntro();
        initAppinfoAD();
    }

    private void initAppinfoAD() {
        frameAppinfoAD = (FrameLayout) findViewById(R.id.frame_gc_detail_appinfo_ad);
        UIUtils.setViewSize(frameAppinfoAD, _linear_appinfo_content_width, -1);
        UIUtils.setViewMargin(frameAppinfoAD, -1, _frame_gc_detail_appinfoad_margin_top, -1, -1);
    }

    private void initAppinfoMain() {
        linearAppinfoMain = (LinearLayout) findViewById(R.id.linear_gc_detail_appinfo_main);
        UIUtils.setViewSize(linearAppinfoMain, _linear_appinfo_content_width, -1);
        // UIUtils.setLayoutParams(linearAppinfoMain, linearAppinfoRoot, -1, -1,
        // _linear_appinfo_content_width, -1, -1, -1, -1, -1);
        initAppinfoMainLeft();
        initAppinfoMainRight();
    }

    private void initAppinfoMainRight() {
        linearAppinfoMainRight = (LinearLayout) findViewById(R.id.linear_gc_detail_appinfo_main_right);
        UIUtils.setViewMargin(linearAppinfoMainRight, _linear_gc_detail_appinfoleft_margin_left,
                -1, -1, -1);
        linearAppInfoContent = (LinearLayout) findViewById(R.id.linear_gc_detail_appinfo_content);
        // txtVersion = (TextView) findViewById(R.id.txt_gc_detail_version);
        // txtVersion.setTextSize(_info_textsize);
        // txtVersion.setTextColor(Color.WHITE);
        // txtSize = (TextView) findViewById(R.id.txt_gc_detail_size);
        // txtSize.setTextSize(_info_textsize);
        // txtSize.setTextColor(Color.WHITE);
        // UIUtils.setViewMargin(txtSize, -1, _info_line_space, -1, -1);
        // txtType = (TextView) findViewById(R.id.txt_gc_detail_type);
        // txtType.setTextSize(_info_textsize);
        // txtType.setTextColor(Color.WHITE);
        // UIUtils.setViewMargin(txtType, -1, _info_line_space, -1, -1);
        // txtDLCount = (TextView) findViewById(R.id.txt_gc_detail_dlcount);
        // txtDLCount.setTextSize(_info_textsize);
        // txtDLCount.setTextColor(Color.WHITE);
        // UIUtils.setViewMargin(txtDLCount, -1, _info_line_space, -1, -1);

        linearAppinfoDevice = (LinearLayout) findViewById(R.id.linear_gc_detail_device);
        UIUtils.setViewMargin(linearAppinfoDevice, -1, _linear_gc_detail_appinfo_device_margin_top,
                -1, -1);
        imgJoystick = (CoocaaDraweeView) findViewById(R.id.img_gc_detail_joystick);
        imgJoystick.setImageURI(Android.getResourceURI(mContext, R.drawable.gc_detail_joystick));
//        imgJoystick.setImageResource(R.drawable.gc_detail_joystick);
        UIUtils.setViewSize(imgJoystick, _device_width, _device_height);
        UIUtils.setViewMargin(imgJoystick, -1, -1, _device_spacing, -1);
        // UIUtils.setLayoutParams(imgJoystick, linearAppinfoDevice, -1, -1, _device_width,
        // _device_height, -1, -1, -1, -1);
        imgMouse = (CoocaaDraweeView) findViewById(R.id.img_gc_detail_mouse);
        imgMouse.setImageURI(Android.getResourceURI(mContext, R.drawable.gc_detail_mouse));
//        imgMouse.setImageResource(R.drawable.gc_detail_mouse);
        UIUtils.setViewMargin(imgMouse, -1, -1, _device_spacing, -1);
        UIUtils.setViewSize(imgMouse, _device_width, _device_height);
        // UIUtils.setLayoutParams(imgMouse, linearAppinfoDevice, -1, -1, _device_width,
        // _device_height, -1, -1, -1, -1);
        imgRemote = (CoocaaDraweeView) findViewById(R.id.img_gc_detail_remote);
        imgRemote.setImageURI(Android.getResourceURI(mContext, R.drawable.gc_detail_remote));
//        imgRemote.setImageResource(R.drawable.gc_detail_remote);
        UIUtils.setViewMargin(imgRemote, -1, -1, _device_spacing, -1);
        UIUtils.setViewSize(imgRemote, _device_width, _device_height);
        // UIUtils.setLayoutParams(imgRemote, linearAppinfoDevice, -1, -1, _device_width,
        // _device_height, -1, -1, -1, -1);

        imgMobile = (CoocaaDraweeView) findViewById(R.id.img_gc_detail_mobile);
        imgMobile.setImageURI(Android.getResourceURI(mContext, R.drawable.gc_detail_phone));
//        imgMobile.setImageResource(R.drawable.gc_detail_phone);
        UIUtils.setViewMargin(imgMobile, -1, -1, _device_spacing, -1);
        UIUtils.setViewSize(imgMobile, _device_width, _device_height);

        imgGun = (CoocaaDraweeView) findViewById(R.id.img_gc_detail_gun);
        imgGun.setImageURI(Android.getResourceURI(mContext, R.drawable.gc_detail_gun));
//        imgMobile.setImageResource(R.drawable.gc_detail_phone);
        UIUtils.setViewMargin(imgGun, -1, -1, _device_spacing, -1);
        UIUtils.setViewSize(imgGun, _device_width, _device_height);

        imgCLeapMotion = (CoocaaDraweeView) findViewById(R.id.img_gc_detail_control_leap_motion);
        imgCLeapMotion.setImageURI(Android.getResourceURI(mContext, R.drawable.gc_detail_camera));
//        imgCLeapMotion.setImageResource(R.drawable.gc_detail_kinect);
        UIUtils.setViewMargin(imgCLeapMotion, -1, -1, _device_spacing, -1);
        UIUtils.setViewSize(imgCLeapMotion, _device_width, _device_height);

        imgILeapMotion = (CoocaaDraweeView) findViewById(R.id.img_gc_detail_induction_leap_motion);
        imgILeapMotion.setImageURI(Android.getResourceURI(mContext, R.drawable.gc_detail_kinect));
//        imgILeapMotion.setImageResource(R.drawable.gc_detail_camera);
        UIUtils.setViewMargin(imgILeapMotion, -1, -1, _device_spacing, -1);
        UIUtils.setViewSize(imgILeapMotion, _device_width, _device_height);
    }

    private void initAppinfoMainLeft() {
        linearAppinfoMainLeft = (LinearLayout) findViewById(R.id.linear_gc_detail_appinfo_main_left);
        imgPoster = (CoocaaDraweeView) findViewById(R.id.img_gc_detail_poster);
        imgPoster.getHierarchy().setPlaceholderImage(R.drawable.game_center_default);
//        imgPoster.setBackgroundResource(R.drawable.game_center_default);
        imgPosterPlay = (CoocaaDraweeView) findViewById(R.id.img_gc_detail_posterplay);
        frameDLCtr = (FrameLayout) findViewById(R.id.frame_gc_detail_appinfo_dlctr);
        UIUtils.setViewSize(imgPoster, _poster_width, Div(256));
        UIUtils.setViewMargin(frameDLCtr, -1, _frame_gc_detail_dlctr_margin_top, -1, -1);
        UIUtils.setViewSize(imgPosterPlay, _posterplay_width, _posterplay_height);
        dlCtrView = new DownloadCtrView(mContext);
        frameDLCtr.addView(dlCtrView);
    }

    private void initAppinfoIntro() {
        txtIntro = (TextView) findViewById(R.id.txt_gc_detail_intro);
        txtIntro.setTextColor(Color.WHITE);
        txtIntro.setLineSpacing(SkyConstants.blockSpacing, 1.0f);
        UIUtils.setViewSize(txtIntro, _linear_appinfo_content_width, -1);
        txtIntro.setTextSize(_intro_textsize);
        UIUtils.setViewMargin(txtIntro, -1, _linear_gc_detail_intro_margin_top, -1, -1);
    }

    public void loadAppInfoBitmap(String bgURL, String posterURL) {
        imgPoster.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.grid_anim));
        FrescoUtils.resize(imgPoster, Uri.parse(posterURL), _poster_width, _poster_height, null);
//        imageCache.load(posterURL, new LiteImageCacheLoadListener() {
//
//            @Override
//            public void onImageLoaded(final LiteImageCacheObject obj) {
//                synchronized (this) {
//                    if (isDestroyed) {
//                        obj.recyle();
//                        return;
//                    }
//                }
//                synchronized (imageCacheObjects) {
//                    imageCacheObjects.add(obj);
//                }
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        try {
//                            imgPoster.setBackgroundDrawable(null);
//                            imgPoster.setImageBitmap(obj.getBitmap());
//                        } catch (Exception e) {
//                            // TODO Auto-generated catch block
//                            e.printStackTrace();
//                        }
//                    }
//                });
//            }
//        }, _poster_width, _poster_height);
        FrescoUtils.resize(imgBG, Uri.parse(bgURL), _bg_width, _bg_height, null);
//        imageCache.load(bgURL, new LiteImageCacheLoadListener() {
//            @Override
//            public void onImageLoaded(final LiteImageCacheObject obj) {
//                synchronized (this) {
//                    if (isDestroyed) {
//                        obj.recyle();
//                        return;
//                    }
//                }
//                synchronized (imageCacheObjects) {
//                    imageCacheObjects.add(obj);
//                }
//                runOnUiThread(new Runnable() {
//                    @SuppressWarnings("deprecation")
//                    @Override
//                    public void run() {
//                        try {
//                            frameRoot.setBackgroundDrawable(new BitmapDrawable(obj.getBitmap()));
//                        } catch (Exception e) {
//                            // TODO Auto-generated catch block
//                            e.printStackTrace();
//                        }
//                    }
//                });
//            }
//        }, _bg_width, _bg_height, 5);
    }

    public void loadBigPosterBitmap(List<BigPoster> urls) {
        bigPosterUrls = urls;
        for (final BigPoster url : bigPosterUrls) {
            final BigPosterImageView poster = new BigPosterImageView(mContext, url);
            final int index = bigPosterUrls.indexOf(url) + 1888;
            poster.setId(index);
            poster.setOnClickListener(DetailView.this);
            relativeBigPoster.addView(poster);
            if (relativeBigPoster.getChildCount() > 1) {
                RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                lp1.addRule(RelativeLayout.BELOW, index - 1);
                poster.setLayoutParams(lp1);
                UIUtils.setViewMargin(poster, -1, _bigposter_spacing, -1, -1);
            }
            if (bigPosterUrls.indexOf(url) == bigPosterUrls.size() - 1) {
                poster.setOnKeyListener(new OnKeyListener() {
                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
                            return true;
                        return false;
                    }
                });
            }
            UIUtils.setViewSize(poster, _bigposter_width, _bigposter_height);
            poster.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.grid_anim));
            FrescoUtils.resize(poster, Uri.parse(url.small), _bigposter_width, _bigposter_height, null);
//            imageCache.load(url.small, new LiteImageCacheLoadListener() {
//                @Override
//                public void onImageLoaded(final LiteImageCacheObject obj) {
//                    synchronized (this) {
//                        if (isDestroyed) {
//                            obj.recyle();
//                            return;
//                        }
//                    }
//                    synchronized (imageCacheObjects) {
//                        imageCacheObjects.add(obj);
//                    }
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            try {
//                                poster.setImageBitmap(obj.getBitmap());
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//                        }
//                    });
//                }
//            }, _bigposter_width, _bigposter_height);
        }
    }

    @Override
    public void initSlideFocusView(SlideFocusView parent) {
        imgPoster.setFocusable(true);
        imgPoster.setFocusableInTouchMode(true);
        parent.focusChangedEvent.registerView(imgPoster, new SlideFocusView.FocusViewRevision(Div(30), Div(80)));
        int child_count = relativeBigPoster.getChildCount();
        for (int i = 0; i < child_count; i++) {
            View view = relativeBigPoster.getChildAt(i);
            view.setFocusable(true);
            view.setFocusableInTouchMode(true);
            parent.focusChangedEvent.registerView(view, new SlideFocusView.FocusViewRevision(Div(36), Div(80)));
        }
        dlCtrView.initSlideFocusView(parent);
    }

    public void setDownloadCtrViewListener(DownloadCtrViewListener l) {
        dlCtrView.setDownloadCtrViewListener(l);
    }

    public void setOnBigPosterClickListener(OnBigPosterClickListener l) {
        bigPosterClickListener = l;
    }

    public void updateStatus(AppStatus status, Object obj) {
        dlCtrView.updateStatus(status, obj);
    }

    public void updateDownloadPercent(int percent) {
        dlCtrView.updateDownloadPercent(percent);
    }

    public void destroy() {
        synchronized (this) {
            isDestroyed = true;
            synchronized (imageCacheObjects) {
                for (LiteImageCacheObject object : imageCacheObjects) {
                    if (object != null)
                        object.recyle();
                }
                imageCacheObjects.clear();
            }
            synchronized (bigPosterMap) {
                Iterator<String> iter = bigPosterMap.keySet().iterator();

                while (iter.hasNext()) {
                    String key = iter.next();
                    Bitmap bitmap = bigPosterMap.get(key);
                    if (bitmap != null && !bitmap.isRecycled()) {
                        bitmap.recycle();
                        bitmap = null;
                    }
                }
                bigPosterMap.clear();
            }
        }
    }

    public void updateAppInfo(final String title, final String pkgName, final String gameVideoUrl, List<String> infos,
                              int device, String intro) {
        if (gameVideoUrl != null && !gameVideoUrl.equals("")) {
            imgPosterPlay.setVisibility(View.VISIBLE);
            imgPoster.setFocusable(true);
            imgPoster.setFocusableInTouchMode(true);
            imgPoster.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!LiteAppApplication.checkNetworkConnection(TipToast.showNetIsNotConnectedToastRunnable))
                        return;

                    try {
                        LogUtils.SubmitLog log = new LogUtils.SubmitLog();
                        log.id = "gc2_game_video_onclick";
                        log.params = new HashMap<String, String>();
                        log.params.put("game_name", title);
                        log.params.put("game_pkgname", pkgName);
                        log.params.put("game_video_id", gameVideoUrl);
                        LogUtils.submit(log);
                        XServiceManager.getLiteCCConnectorService().startPlayer(title, gameVideoUrl, false);
//                        ReadyInfo info = new ReadyInfo();
//                        info.name = title;
//                        XServiceManager.getLitePlayerClientService().getPlayer().getReady(info);
//                        VideoSourceInfo vInfo = new VideoSourceInfo();
//                        vInfo.url = gameVideoUrl;
//                        XServiceManager.getLitePlayerClientService().getPlayer()
//                                .start(vInfo, title);
                    } catch (RemoteException e) {
                    }
                }
            });
        } else {
            imgPosterPlay.setVisibility(View.GONE);
            imgPoster.setFocusable(false);
            imgPoster.setFocusableInTouchMode(false);
        }
        for (String info : infos) {
            TextView textView = new TextView(mContext);
            textView.setTextSize(_info_textsize);
            textView.setTextColor(Color.WHITE);
            textView.setText(info);
            linearAppInfoContent.addView(textView);
            if (infos.indexOf(info) > 0)
                UIUtils.setViewMargin(textView, -1, _info_line_space, -1, -1);
        }

        // txtVersion.setText(mContext.getResources().getString(R.string.gc_detail_version,
        // version));
        // txtSize.setText(mContext.getResources().getString(R.string.gc_detail_size,
        // Size2String.byte2Auto(size)));
        // txtType.setText(mContext.getResources().getString(R.string.gc_detail_type, type));
        // txtDLCount.setText(mContext.getResources().getString(R.string.gc_detail_dlcount,
        // dlcount));

        if (SUPPORT_DEVICE.JOYSTICK.isSupport(device))
            imgJoystick.setVisibility(View.VISIBLE);
        else
            imgJoystick.setVisibility(View.GONE);

        if (SUPPORT_DEVICE.MOUSE.isSupport(device))
            imgMouse.setVisibility(View.VISIBLE);
        else
            imgMouse.setVisibility(View.GONE);

        if (SUPPORT_DEVICE.REMOTE.isSupport(device))
            imgRemote.setVisibility(View.VISIBLE);
        else
            imgRemote.setVisibility(View.GONE);

        if (SUPPORT_DEVICE.MOBLIE.isSupport(device))
            imgMobile.setVisibility(View.VISIBLE);
        else
            imgMobile.setVisibility(View.GONE);

        if (SUPPORT_DEVICE.GUN.isSupport(device))
            imgGun.setVisibility(View.VISIBLE);
        else
            imgGun.setVisibility(View.GONE);


        if (SUPPORT_DEVICE.CONTROL_LEAP_MOTION.isSupport(device))
            imgCLeapMotion.setVisibility(View.VISIBLE);
        else
            imgCLeapMotion.setVisibility(View.GONE);

        if (SUPPORT_DEVICE.INDUCTION_LEAP_MOTION.isSupport(device))
            imgILeapMotion.setVisibility(View.VISIBLE);
        else
            imgILeapMotion.setVisibility(View.GONE);

        txtIntro.append(intro);
    }

    public void updateAD(View view) {
        frameAppinfoAD.removeAllViews();
        frameAppinfoAD.addView(view);
    }

    private static class BigPosterImageView extends CoocaaDraweeView {
        public BigPoster url = null;

        public BigPosterImageView(Context context, BigPoster url) {
            super(context);
            this.url = url;
            setFocusable(true);
            setFocusableInTouchMode(true);
            getHierarchy().setPlaceholderImage(R.drawable.game_center_default);
        }
    }

    private ImageGallery ig = null;

    @Override
    public void onClick(View v) {
        try {
            BigPosterImageView poster = (BigPosterImageView) v;
            if (bigPosterClickListener != null)
                bigPosterClickListener.onBigPosterClick(poster.url);

            int index = bigPosterUrls.indexOf(poster.url);
            if (ig == null) {
                ig = new ImageGallery(CoocaaActivity.getCurrentActivityContext());
                ig.setImageGalleryListener(l);
                ig.setImageGalleryController(imageGalleryController);
            }
            Bundle bundle = new Bundle();
            bundle.putInt("index", index);
            ig.show(bundle);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private ImageGallery.ImageGalleryController imageGalleryController = new ImageGallery.ImageGalleryController() {

        private LinearLayout indicator;
        private ImageView downArrow, upArrow;
        private TextView indicatorText;

        @Override
        protected void onCreate() {
            super.onCreate();
            setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.RIGHT));
            indicator = new LinearLayout(CoocaaApplication.getContext());
            SkinManager.setBackground(indicator, R.drawable.gc_sd_game_indicator_bg);
            indicator.setPadding(SkyConstants.Div(6), SkyConstants.Div(60), 0, SkyConstants.Div(60));
            indicator.setGravity(Gravity.CENTER_VERTICAL);
            indicator.setOrientation(LinearLayout.VERTICAL);
            addView(indicator);

            downArrow = new ImageView(CoocaaApplication.getContext());
            upArrow = new ImageView(CoocaaApplication.getContext());
            SkinManager.setBackground(downArrow, R.drawable.gc_sd_down_arrow_shot);
            SkinManager.setBackground(upArrow, R.drawable.gc_sd_up_arrow_shot);

            indicatorText = UICommon.newTextView(CoocaaApplication.getContext(), SkyConstants.Dpi(30), R.color.defaultColor);
            indicatorText.setPadding(0, SkyConstants.Div(30), 0, SkyConstants.Div(30));
            indicator.addView(upArrow);
            indicator.addView(indicatorText);
            indicator.addView(downArrow);
        }

        @Override
        public void onImageInit(ImageGallery gallery, final ImageGallery.ImageUri current, final List<ImageGallery.ImageUri> uris, Bundle bundle) {
            LiteHandlerThread.postOnUiThread(new Runnable() {
                @Override
                public void run() {
                    indicatorText.setText(String.format("%d/%d", uris.indexOf(current) + 1, uris.size()));
                }
            });
        }

        @Override
        public void onImageSwitchStart(ImageGallery gallery, final ImageGallery.ImageUri from, final ImageGallery.ImageUri to, final List<ImageGallery.ImageUri> uris, Bundle bundle) {
            LiteHandlerThread.postOnUiThread(new Runnable() {
                @Override
                public void run() {
                    indicatorText.setText(String.format("%d/%d", uris.indexOf(to) + 1, uris.size()));
                }
            });
        }

        @Override
        public void onImageSwitchEnd(ImageGallery gallery, ImageGallery.ImageUri current, List<ImageGallery.ImageUri> uris, Bundle bundle) {

        }


        @Override
        public Animation getInAnimation() {
            return ImageGallery.ANIMATION_IN_RIGHT_LEFT;
        }

        @Override
        public Animation getOutAnimation() {
            return ImageGallery.ANIMATION_OUT_LEFT_RIGHT;
        }
    };

    private ImageGallery.ImageGalleryListener l = new ImageGallery.ImageGalleryListener() {

        @Override
        public void onImageGalleryCreate(ImageGallery gallery, Bundle bundle) {

            List<ImageGallery.ImageUri> uris = new ArrayList<ImageGallery.ImageUri>();
            for (BigPoster p : bigPosterUrls)
                uris.add(new ImageGallery.ImageUri(R.drawable.game_center_default, Uri.parse(p.small), Uri.parse(p.big)));
            if (bundle != null)
                ig.loadImageSource(uris, bundle.getInt("index"));
            else
                ig.loadImageSource(uris, 0);
        }

        @Override
        public void onImageGalleryStop(ImageGallery gallery, Bundle bundle) {
            ig = null;
        }

        @Override
        public boolean onImageGalleryKeyDown(ImageGallery gallery, int keyCode, Bundle bundle) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_DPAD_UP:
                    gallery.showPrevious(ImageGallery.ANIMATION_IN_UP_DOWN, ImageGallery.ANIMATION_OUT_UP_DOWN);
                    return true;
                case KeyEvent.KEYCODE_DPAD_DOWN:
                    gallery.showNext(ImageGallery.ANIMATION_IN_DOWN_UP, ImageGallery.ANIMATION_OUT_DOWN_UP);
                    return true;
                default:
                    break;
            }
            return false;
        }

        @Override
        public void onImageClick(ImageGallery gallery, ImageGallery.ImageUri uri, Bundle bundle) {

        }
    };

    private Map<String, Bitmap> bigPosterMap = new HashMap<String, Bitmap>();

//    @Override
//    public Bitmap getBitmapByIndex(int index) {
//        String url = bigPosterUrls.get(index).small;
//        LiteImageCache.getInstance().load(url, new LiteImageCacheLoadListener() {
//            @Override
//            public void onImageLoaded(LiteImageCacheObject obj) {
//                if (obj == null) {
//                    synchronized (bigPosterMap) {
//                        bigPosterMap.notify();
//                    }
//                    return;
//                }
//                synchronized (bigPosterMap) {
//                    synchronized (this) {
//                        if (isDestroyed) {
//                            obj.recyle();
//                            return;
//                        }
//                    }
//                    Bitmap bitmap = bigPosterMap.get(obj.getUrl());
//                    if (bitmap != null && !bitmap.isRecycled())
//                        bigPosterMap.notify();
//                    else {
//                        try {
//                            bitmap = obj.getBitmap();
//                            synchronized (imageCacheObjects) {
//                                imageCacheObjects.add(obj);
//                            }
//                            bigPosterMap.put(obj.getUrl(), bitmap);
//                            bigPosterMap.notify();
//                        } catch (Exception e) {
//                            // TODO Auto-generated catch block
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            }
//        }, _bigposter_width, _bigposter_height);
//        Bitmap bitmap = null;
//        synchronized (bigPosterMap) {
//            bitmap = bigPosterMap.get(url);
//            if (!(bitmap != null && !bitmap.isRecycled())) {
//                try {
//                    bigPosterMap.wait();
//                    bitmap = bigPosterMap.get(url);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        return bitmap;
//    }

    public void setFocus() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                dlCtrView.setFocus();
            }
        }, 500);
    }

    public void setThumbV(int id, ScrollView v) {
        try {
            Field mScrollCacheField = View.class.getDeclaredField("mScrollCache");
            mScrollCacheField.setAccessible(true);
            Object mScrollCache = mScrollCacheField.get(v); // scr is your Scroll View

            Field scrollBarField = mScrollCache.getClass().getDeclaredField("scrollBar");
            scrollBarField.setAccessible(true);
            Object scrollBar = scrollBarField.get(mScrollCache);

            Method method = scrollBar.getClass().getDeclaredMethod("setVerticalThumbDrawable",
                    Drawable.class);
            method.setAccessible(true);
            // Set your drawable here.
            method.invoke(scrollBar, getResources().getDrawable(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
